package com.yt.server.db.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * sqlite数据集
 */
public class SqliteDBPool implements DBPool {

	private String jdbcUrl;

	private String driverClass;

	@Override
	public Connection getConnection() {
		try {
			Class.forName(driverClass);
			Connection conn = DriverManager.getConnection(jdbcUrl);
			return conn;
		} catch (Exception e) {
			System.err.println(jdbcUrl + ":连接失败!");
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void init(String dbUserName, String dbPassWord, String jdbcUrl,
			String driverClass) {
		init(dbUserName, dbPassWord, jdbcUrl, driverClass, 0, 0, 0, 0, 0);
	}

	@Override
	public void init(String dbUserName, String dbPassWord, String jdbcUrl,
			String driverClass, int initSize, int minPoolSize, int maxPoolSize,
			int maxStatements, int maxIdleTime) {
		this.jdbcUrl = jdbcUrl;
		this.driverClass = driverClass;
	}

	public static void main(String[] args) {
		try {
			SqliteDBPool pool = new SqliteDBPool();
			pool.init("", "", "jdbc:sqlite:C:\\sqlite\\test.db",
					"org.sqlite.JDBC");
			Connection conn = pool.getConnection();
			Statement stat = conn.createStatement();
			
			stat.executeUpdate("create table cache(sql varchar(20000),jndi varchar(50), result varchar(60000),time varchar(20));");
			stat.executeUpdate("drop table cache;");
			
			stat.executeUpdate("create table tbl1(name varchar(20), salary int);");
			// 插入数据
			stat.executeUpdate("insert into tbl1 values('ZhangSan',8000);");
			stat.executeUpdate("insert into tbl1 values('LiSi',7800);");
			stat.executeUpdate("insert into tbl1 values('WangWu',5800);");
			stat.executeUpdate("insert into tbl1 values('ZhaoLiu',9100);");
			ResultSet rs = stat.executeQuery("select * from tbl1;"); // 查询数据
			while (rs.next()) { // 将查询到的数据打印出来
				System.out.print("name = " + rs.getString("name") + " "); // 列属性一
				System.out.println("salary = " + rs.getString("salary")); // 列属性二
			}
			rs.close();
			conn.close(); // 结束数据库的连接
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public int getNumConnection() throws SQLException {
		return 0;
	}

	@Override
	public int getNumIdleConection() throws SQLException {
		return 0;
	}

	@Override
	public int getNumBusyConnection() throws SQLException {
		return 0;
	}

	@Override
	public boolean testCheckoutConnection() throws SQLException {
		return true;
	}
}